package com.ygqh.baby.solr;

import com.alibaba.fastjson.JSON;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

public class SolrApi {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private SolrClient solrClient = YgSolrClient.INSTANCE.getSolrClient();

    public UpdateResponse add(Object obj) throws SolrServerException, IOException {
        logger.info("add param:【{}】", JSON.toJSONString(obj));
        UpdateResponse response = solrClient.addBean(obj);
        solrClient.commit();
        logger.info("add response:【{}】", response.toString());
        return response;
    }

    public <T> UpdateResponse addBatch(List<T> list) throws SolrServerException, IOException {
        logger.info("addBatch param:【{}】", JSON.toJSONString(list));
        UpdateResponse response = solrClient.addBeans(list);
        solrClient.commit();
        logger.info("addBatch response:【{}】", response.toString());
        return response;
    }

    public UpdateResponse del(String id) throws SolrServerException, IOException {
        logger.info("del param:【{}】", id);
        UpdateResponse response = solrClient.deleteById(id);
        solrClient.commit();
        logger.info("del response:【{}】", response.toString());
        return response;
    }

    public UpdateResponse del(List<String> ids) throws SolrServerException, IOException {
        logger.info("delBatch param:【{}】", JSON.toJSONString(ids));
        UpdateResponse response = solrClient.deleteById(ids);
        solrClient.commit();
        logger.info("delBatch response:【{}】", response.toString());
        return response;
    }

}
